<template>
  <div>
    <div class="header" unselectable="on">
      <div class="head_total">
        <span class="name">已发送</span>
        <span class="num">（共{{this.total}}封）</span>
      </div>
      <div>
        <div class="page_num">{{this.pageNum}}/{{this.pages}}</div>
        <div class="page_btns">
          <div :class="['lf_btn', pageNum>1?'':'nui-btn-disabled']" @click="pageNum>1?changeNum('cut'):''">
            <span class="nui-btn-icon">
              <b class="nui-ico"></b>
            </span>
            <span class="nui-btn-text">上一页</span>
          </div>
          <div :class="['rt_btn', pageNum==pages?'nui-btn-disabled':'']" @click="pageNum==pages?'':changeNum('add')">
            <span class="nui-btn-icon">
              <b class="nui-ico"></b>
            </span>
            <span class="nui-btn-text">下一页</span>
          </div>
        </div>
      </div>
    </div>
    <div class="list">
      <el-table
        class="table"
        v-loading='loading'
        :data="list"
        tooltip-effect="dark"
        @row-click="goDetail($event)">
        <el-table-column
          label="@"
          width="35">
          <template>
            <i class="green el-icon-message" title="发送成功"></i>
          </template>
        </el-table-column>
        <el-table-column
          label="收件人"
          width="180">
          <template slot-scope="scope">
            {{scope.row.toNick ? scope.row.toNick : '未填写'}}
          </template>
        </el-table-column>8
        <el-table-column
          show-overflow-tooltip
          label="主题">
          <template slot-scope="scope">
            {{scope.row.title ? scope.row.title : '无主题'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="created"
          fixed="right"
          width="170"
          label="时间">
        </el-table-column>
        <el-table-column
          fixed="right"
          width="200"
          label="操作">
          <template slot-scope="scope">
            <el-button type="info" plain size="mini" @click="toDelete(scope.row.id, $event)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
  import { mailList, mailEdi } from '../../apis/api.js'
  import { storage } from '../../utils/storage.js'
  export default{
    data() {
      return {
        list: [],
        loading: false,
        total: 0,
        pages: 1,
        pageNum: 1,
        pageSize: 10,
      }
    },
    methods: {
      getList() {
        this.loading = true;
        mailList({
          show: '1',
          type: '1',
          userId: storage.getSession('id'),
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }).then(res => {
          console.log(res)
          this.list = res.list;
          this.total = res.total;
          this.pageNum = res.pageNum;
          this.pages = 
            (res.total < res.pageSize) ? 1 : ((res.total % res.pageSize) ? (parseInt(res.total / res.pageSize) + 1) : (res.total / res.pageSize));
          this.loading = false;
        })
      },
      toDelete(id, e) {
        e.stopPropagation();
        mailEdi({
          isRead: '2',
          mailId: id,
          type: '3'
        }).then(res => {
          this.$message.success('邮件删除成功')
          this.getList();
        })
      },
      goDetail(item) {
        console.log(item)
        this.$router.push(`./det/${item.id}`);
      },
      changeNum(type) {
        console.log(type)
        if(type == 'add') {
          this.pageNum++;
        } else if (type == 'cut') {
          this.pageNum--;
        }
        this.getList()
      }
    },
    mounted() {
      this.getList();
    },
  }
</script>

<style scoped>
i.green {
  color: #4DA938;
}
</style>